﻿<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>将HTML转换为JavaScript脚本</title>
    <script type="text/javascript">
        function toScript(val){
            var value = val.value;
            // 特殊字符的转换
            value  = value.replace(/\\/gi,"\\\\").replace(/"/gi,"\\\"").replace(/'/gi,"\\\'");
            valArr = value.split("\r\n");//分行
            value ="";
            for(i = 0;i< varArr.length;i++){
                value += (i == 0) ? "info =" : "";
                value += " \""+ valArr[i];
                value += (i!=valArr.length-1) ? "\" +\"\\n\"+\n" : "\"\n" ;//字符串的间隔符号
            }
            value+="\ndocument.write(info)";
            val.value = value;
        }
    </script>
</head>
<body>
<input type=button value="将 HTML 转为 JavaScript" onclick=toScript(document.all["mytxt"])><br>
<textarea id="mytxt" cols=60 rows=8>
    <table width="300">
    <tr><td align="right">A</td></tr>
    </table>
</textarea>
</body>
</html>